import { join } from 'path';
import express from 'express';
import { serve, setup } from 'swagger-ui-express';
import swaggerDoc from 'swagger-jsdoc';

//配置swagger-jsdoc
const options = {
  definition: {
    openapi: '3.0.0',
    info: {
      title: 'api',
      version: '1.0.0',
      description: `api`,
    },
  },
  // 去哪个路由下收集 swagger 注释
  apis: ['../router/*.js', '../admin/*.js'],
};

var swaggerJson = function (req, res) {
  res.setHeader('Content-Type', 'application/json');
  res.send(swaggerSpec);
};
const swaggerSpec = swaggerDoc(options);

var swaggerInstall = function (app) {
  if (!app) {
    app = express();
  }
  // 开放相关接口，
  app.get('/swagger.json', swaggerJson);
  // 使用 swaggerSpec 生成 swagger 文档页面，并开放在指定路由
  app.use('/swagger', serve, setup(swaggerSpec));
};

export default swaggerInstall;
